package com.test.service;

import com.alibaba.fastjson.JSONObject;
import com.test.Iservice.IExampleService;
import com.test.common.exception.MyException;
import com.test.common.utils.GetTodayWeatherUtil;
import com.test.common.vo.ExceptionEnum;
import com.test.mapper.ExampleMapper;
import com.test.pojo.example;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Description IExampleService实现类
 * @Author Mr.Li
 * @Date 2020/3/10 15:20
 */
@Configuration
public class ExampleServiceImpl implements IExampleService {
    @Autowired
    ExampleMapper exampleMapper;
    /**
    *@Description: 查询全部
    *@Params
    *@Return
    *@Author Mr.Li
    *@Date 2020/3/10 15:47
    */
    @Override
    public List<example> selectAll() {
        List<example> examples = exampleMapper.selectAll();
        if (!CollectionUtils.isEmpty(examples)) {
            return examples;
            }
        throw new MyException(ExceptionEnum.RESULT_IS_EMPTY);
        }

        /**
        *@Description: 删除
        *@Params
        *@Return
        *@Author Mr.Li
        *@Date 2020/3/10 15:47
        */
    @Override
    public Boolean deleteExampleById(Integer id) {
       if(id != null){
           int delete = exampleMapper.deleteExampleById(id);
          if (delete>0)
              return true;

       }
       throw new MyException(ExceptionEnum.INPUT_IS_BLANK);
    }
    /**
    *@Description: 修改
    *@Params
    *@Return
    *@Author Mr.Li
    *@Date 2020/3/10 15:47
    */
    @Override
    public Boolean updateExampleById(example example) {
        if(example != null){
            int update = exampleMapper.updateExampleById(example);
            if (update>0)
                return true;

        }
        throw new MyException(ExceptionEnum.INPUT_IS_BLANK);
    }
    /**
    *@Description: 新增
    *@Params
    *@Return
    *@Author Mr.Li
    *@Date 2020/3/10 15:48
    */
    @Override
    public Boolean addExample(example example) {

        if(example != null){
            int add = exampleMapper.addExample(example);

           if(add>0)
               return true;
        }

        throw new MyException(ExceptionEnum.INPUT_IS_BLANK);
    }

    @Autowired
    GetTodayWeatherUtil GetTodayWeatherUtil;//注入天气类
    /**
    *@Description: 根据城市名称获取天气
    *@Params
    *@Return
    *@Author Mr.Li
    *@Date 2020/3/10 16:52
    */
    @Override
    public JSONObject getWeatherByCityId(String city) {

       if (StringUtils.isNotBlank(city)) {
           JSONObject TodayWeather = GetTodayWeatherUtil.getWeatherInform(city);//调用getWeather获取返回的json对象

           if (!ObjectUtils.isEmpty(TodayWeather))
               return TodayWeather;
       }

       throw new MyException(ExceptionEnum.INPUT_IS_BLANK);
    }
}
